package com.mty.cc.controller;


import com.mty.cc.po.User;
import com.mty.cc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/*
 * @description: 控制层
 * @author: mty
 */
@Controller
@RequestMapping(value = "/user")
class UserController {


    @Autowired(required=false)
    private UserService userService;

    /*
     * @description: 跳转到首页
     * @author: mty
     */
    @RequestMapping("/userIndex")
    public String userIndex(HttpServletRequest request,Model model) throws Exception{
        List<User> userList = userService.queryByAll();
        int total = userList.size();
        model.addAttribute("userList", userList);
        model.addAttribute("total", total);
        return "user/UserIndex";
    }


    /*
     * @description:进入修改
     * @author: mty
     */
    @RequestMapping("/userEdit/{id}")
    public ModelAndView  userEdit(@PathVariable("id") String id,Model model) throws Exception{
        ModelAndView mv = new ModelAndView();
        User user = userService.queryById(id);
        model.addAttribute("user", user);
        mv.setViewName("user/UserEdit");
        return mv;
    }

    /*
     * @description:修改
     * @author: mty
     */
    @RequestMapping("/userEditSubmit")
    public String  userEditSubmit(User user,Model model,HttpServletRequest request) throws Exception{
        try{
            userService.update(user);
            request.setAttribute("msg", "修改成功！");
            return "user/UserEdit";
        }catch (Exception e){
            request.setAttribute("msg", "修改失败！");
            return "user/UserEdit";
        }
    }

    /*
     * @description:进入添加
     * @author: mty
     */
    @RequestMapping("/userAdd")
    public String  userAdd(Model model,HttpServletRequest request) throws Exception{
        return "user/UserAdd";
    }


    /*
     * @description:增加
     * @author: mty
     */
    @RequestMapping("/userAddSub")
    public String  userAddSub(User user,Model model,HttpServletRequest request) throws Exception{
        try{
            userService.add(user);
            request.setAttribute("msg", "添加成功！");
            return "user/UserAdd";
        }catch (Exception e){
            request.setAttribute("msg", "添加失败！");
            return "user/UserAdd";
        }
    }

    /*
     * @description:根据ID删除
     * @author: mty
     */
    @RequestMapping("/userDel/{id}")
    public ModelAndView userDel(@PathVariable("id") String id,HttpServletRequest reuqest, Model model) throws Exception{
        ModelAndView mv = new ModelAndView();
        userService.deleteById(id);
        mv.setViewName("user/UserIndex");
        return mv;
    }

}